<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Common\Api\UserApi;
use Admin\Common\POMVIPClient;
use Admin\Common\Excel;
class MyWarehouseController extends BaseController {
    private $adminUser = null;
    public function _initialize(){
    	parent::_initialize();
    	$this->adminUser = new UserApi();

    }
	//获取后台管理员的信息
    public function index(){
      $jump_stock =I('request.jump_stock','','trim');
      $this->assign('jump_stock',$jump_stock);
      $this->display();
    }

   public function MyWarehouseList(){
     $jump_stock =I('request.jump_stock','','trim');
        $title =I('request.title','','trim');
            if(!empty($title)){
               $where['a.goods_sn'] =array('like',"%{$title}%");
            }
        $bar_code =I('request.bar_code','','trim');
            if(!empty($bar_code)){
               $where['a.bar_code'] =array('like',"%{$bar_code}%");
            }
       $goods_name =I('request.goods_name','','trim');
          if(!empty($goods_name)){
             $where['a.goods_name'] =array('like',"%{$goods_name}%");
          }
       $start_time = I('request.start_time','','trim');
       $end_time = I('request.end_time','','trim');

        //如果两个为空，没有条件
        //否则 一个为空，一个有值，两个有值 
        if(!(empty($start_time) && empty($end_time))){ 
            $start_time = empty($start_time)?date("Y-m-d"):$start_time;
            $end_time = empty($end_time)?date("Y-m-d"):$end_time;
            if($start_time>$end_time){
                 $temp=$end_time;
                 $end_time=$start_time;
                 $start_time=$temp;
            }  
         $start_time = strtotime($start_time);
         $end_time = strtotime($end_time);
          $where['a.create_time'] = array(between,array($start_time,$end_time));
        } 
         $where['a.is_deleted'] = 0;
      
        $order = "a.create_time desc";
        if(I('request.order') != "" && I('request.sort') != ""){
          $order = I('request.order','','trim').' '.I('request.sort','','trim');
          if(I('request.order','','trim') == 'actual'){
            //按实际库存排序
            $order = '(quantity -salenumber) '.' '.I('request.sort','','trim');
          }
          if(I('request.order','','trim') == 'sum'){
            //按实际库存z总价格排序
            $order = '(b.quantity *a.goods_price) '.' '.I('request.sort','','trim');
          }
        }

        if($jump_stock ==1){
           $order = '(quantity -salenumber) '.' ASC';
        }

        // $order = empty(I('request.order','','trim'))?"a.create_time desc":I('request.order','','trim');
                 //中文搜索商品
         $chinese_goods_name = I('request.chinese_goods_name','','trim');
         if($chinese_goods_name!=''){
             $where['a.goods_cn_name'] = array('like','%'.$chinese_goods_name.'%');
         }
        
        $_where_order_field["listrows"] =I("request._listrows");
        $_where_order_field["where"] = $where;
        $_where_order_field["field"] = 'a.*,b.quantity,b.salenumber,(a.goods_price*b.quantity) as sum_money';
        $_where_order_field['alias']="a";
        $_where_order_field["order"] = $order;
        $_where_order_field["join"] =array('left join vip_inventory b on a.goods_sn = b.goods_sn');
        $_datalist= $this->_getListsFromModel('goods',$_where_order_field);
        $sum = array();

       foreach ($_datalist['lists'] as $key => $value) {
          //计算当前页的虚拟仓总金额
           $sum['fictitious_money'] += Number_format($value['quantity']*$value['goods_price'], 2, '.','');
          //计算当前页的代发货总金额
           $sum['available_for_sale_money'] += Number_format($value['salenumber']*$value['goods_price'], 2, '.','');
          //计算当前页的可售总金额
           $sum['wait_for_deliver_money'] +=Number_format( ($value['quantity'] -$value['salenumber']) *$value['goods_price'], 2, '.','');
           

           //获取商品中文名
           // $goods_sn_str .=  empty($goods_sn_str)? $value['goods_sn'] : ','.$value['goods_sn'];
        }

       // print($goods_sn_str);exit;
/*        $POMVIPClient = new POMVIPClient();
        $goods_chinese_name = $POMVIPClient->GoodsSnGetChineseGoodsName($goods_sn_str);
        foreach ($_datalist['lists'] as $k => $v) {
             $v['chinese_goods_name'] = $goods_chinese_name[$k]['chinese_goods_name'];
             $_datalist['lists'][$k]=$v;
        }*/
         // print_r($_datalist['lists']);exit;
        //获取虚拟仓总价格
        $all_money = D('goods')
        ->field('sum(a.goods_price*b.quantity) as all_money ,sum(a.goods_price*(b.quantity-b.salenumber) ) as wait_for_all_money ,sum(a.goods_price*b.salenumber ) as available_for_all_money')
        ->alias('a')
        ->join('left join vip_inventory as b on a.goods_sn = b.goods_sn')
        ->select();
        $wheres = array('title'=>$title,'start_time'=>$start_time,'end_time'=>$end_time,'bar_code'=>$bar_code,'goods_name'=>$goods_name,'_listrows'=>I("request._listrows"),'gotoPage'=>I("request.gotoPage"));
        $this->assign('where',json_encode($wheres));
        $this->assign('order',I('request.order') );
        $this->assign('sort',I('request.sort') );
        $this->assign('sum',$sum);
        $this->assign('all_money',$all_money[0] );
        $this->assign('jump_stock',$jump_stock);
        $this->assign('lists',$_datalist);
        $this->display();
   }



   //导出excel
   public function export_excel(){
        $title =I('request.title','','trim');
            if(!empty($title)){
               $where['a.goods_sn'] =array('like',"%{$title}%");
            }
        $bar_code =I('request.bar_code','','trim');
            if(!empty($bar_code)){
               $where['a.bar_code'] =array('like',"%{$bar_code}%");
            }
       $goods_name =I('request.goods_name','','trim');
          if(!empty($goods_name)){
             $where['a.goods_name'] =array('like',"%{$goods_name}%");
          }
       $start_time = I('request.start_time','','trim');
       $end_time = I('request.end_time','','trim');

        //如果两个为空，没有条件
        //否则 一个为空，一个有值，两个有值 
        if(!(empty($start_time) && empty($end_time))){ 
            $start_time = empty($start_time)?date("Y-m-d"):$start_time;
            $end_time = empty($end_time)?date("Y-m-d"):$end_time;
            if($start_time>$end_time){
                 $temp=$end_time;
                 $end_time=$start_time;
                 $start_time=$temp;
            }  
         $start_time = strtotime($start_time);
         $end_time = strtotime($end_time);
          $where['a.create_time'] = array(between,array($start_time,$end_time));
        } 
         $where['a.is_deleted'] = 0;
     $chinese_goods_name = I('request.chinese_goods_name','','trim');
         if($chinese_goods_name!=''){
             $where['a.goods_cn_name'] = array('like','%'.$chinese_goods_name.'%');
         }
        
      $list = D('goods')->alias('a')->field('a.*,b.quantity')->where($where)->join('left join vip_inventory b on a.goods_sn = b.goods_sn')->order('create_time desc')->select();
     $data = array(array('序号','商品自编码','商品条形码','商品名称','价格($)','含GST($)','库存余额','采购时间'));
      foreach ($list as $key => $value) {
        $array=array();

        array_push($array,$key+1,$value['goods_sn'],$value['bar_code'],$value['goods_name'],$value['goods_price'],$value['taxrate'],$value['quantity'],date('Y-m-d H:i:s',$value['create_time']));
        array_push($data,$array);
      }
       $title = "我的仓库记录".date('Y-m-d H:i');
      $excel = new Excel();
      header("Content-type:text/html;charset=UTF-8");
      $excel->download($data,$title,'');
   }


}
